package GUILayer;
import CtrlLayer.*;
import ModelLayer.Employee;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class PopupRedigerMedarbejder extends PopupWindow {
	private PersonCtrl PC;
	private Employee myemployee;
	private JTextField textUsername;
	private JTextField textNavn;
	private JTextField textAdresse;
	private JTextField textPostnr;
	private JTextField textBy;
	private JTextField textTelefon;
	private JTextField textEmail;
	private JTextField textPassword;
	
	public PopupRedigerMedarbejder(Updateable mother)
	{
		super(mother);
		PC = new PersonCtrl();
		
		getContentPane().setPreferredSize(new Dimension(440, 350));
		setTitle("Rediger medarbejder");
        
        JLabel lblNavn = new JLabel("Navn");
        JLabel lblAdresse = new JLabel("Adresse");
        JLabel lblPostnr = new JLabel("Postnr.");
        JLabel lblBy = new JLabel("By");
        JLabel lblTelefon = new JLabel("Telefon");
        JLabel lblEmail = new JLabel("Email");
        JLabel lblUsername = new JLabel("Brugernavn");
        JLabel lblPassword = new JLabel("Kodeord");
        
        JButton btnAnnuller = new JButton("Annuller");
        btnAnnuller.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e)
        	{
        		quit();
        	}
        });
        JButton btnRet = new JButton("Ret");
        btnRet.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0)
        	{
        		opretMedarbejder();
        		getMother().update();
				quit();
        	}
        });
        
        textNavn = new JTextField();
        textNavn.setColumns(10);
        
        textAdresse = new JTextField();
        textAdresse.setColumns(10);
        
        textPostnr = new JTextField();
        textPostnr.setColumns(10);
        
        textBy = new JTextField();
        textBy.setColumns(10);
        
        textTelefon = new JTextField();
        textTelefon.setColumns(10);
        
        textEmail = new JTextField();
        textEmail.setColumns(10);
        
        textUsername = new JTextField();
        textUsername.setColumns(10);
        
        textPassword = new JTextField();
        textPassword.setColumns(10);
        
        JButton btnSg = new JButton("Søg");
        btnSg.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0)
        	{
        		getMedarbejder();
        	}
        });
           
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addGap(28)
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(lblUsername)
        				.addComponent(lblNavn)
        				.addComponent(lblAdresse)
        				.addComponent(lblPostnr)
        				.addComponent(lblBy)
        				.addComponent(lblTelefon, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblEmail)
        				.addComponent(lblPassword))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(textPassword, GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
        				.addGroup(groupLayout.createSequentialGroup()
        					.addComponent(btnAnnuller)
        					.addPreferredGap(ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
        					.addComponent(btnSg)
        					.addGap(40)
        					.addComponent(btnRet))
        				.addComponent(textEmail, 288, 288, 288)
        				.addComponent(textTelefon, 288, 288, 288)
        				.addComponent(textBy, 288, 288, 288)
        				.addComponent(textPostnr, 288, 288, 288)
        				.addComponent(textAdresse, 288, 288, 288)
        				.addComponent(textNavn, 288, 288, 288)
        				.addComponent(textUsername, GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))
        			.addGap(30))
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblNavn)
        				.addComponent(textNavn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblAdresse)
        				.addComponent(textAdresse, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblPostnr)
        				.addComponent(textPostnr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblBy, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textBy, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblTelefon)
        				.addComponent(textTelefon, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblEmail)
        				.addComponent(textEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblUsername)
        				.addComponent(textUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblPassword)
        				.addComponent(textPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(27)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(btnAnnuller)
        				.addComponent(btnRet)
        				.addComponent(btnSg))
        			.addContainerGap())
        );
        
        getContentPane().setLayout(groupLayout);
        pack();
	}
	
	private void opretMedarbejder()	//indlaeser text fra skaermen
    {
        String name = textNavn.getText();
        String address = textAdresse.getText();
        String postal = textPostnr.getText();
        String city = textBy.getText();
        String phone = textTelefon.getText();
        String email = textEmail.getText();
        String username = textUsername.getText();
        String password = textPassword.getText();
        PC.createEmployee(name, address, postal, city, phone, email, username, password);
    }
	
	public void getMedarbejder()
	{
		String phone = textTelefon.getText();
		myemployee = PC.searchEmployee(phone);
		
		textNavn.setText( myemployee.getName() );
		textAdresse.setText( myemployee.getAddress() );
		textPostnr.setText( myemployee.getPostal() );
		textBy.setText( myemployee.getCity() );
		textTelefon.setText( myemployee.getPhone() );
		textEmail.setText( myemployee.getEmail() );
		textUsername.setText( myemployee.getUsername() );
		textPassword.setText( myemployee.getPassword() );
	}
	
	public void resetMedarbejder()
	{
		textNavn.removeAll();
		textAdresse.removeAll();
		textPostnr.removeAll();
		textBy.removeAll();
		textTelefon.removeAll();
		textEmail.removeAll();
		textUsername.removeAll();
		textPassword.removeAll();
	}

}
